package com.example.common;

import org.springframework.util.DigestUtils;

import java.util.UUID;

public class Passwordutil {
    //给密码加盐
    public static String enctypt(String password){
        //随机得到盐值
       String salt= UUID.randomUUID().toString().replace("-","");
       String mdpassword= DigestUtils.md5DigestAsHex((password+salt).getBytes());
       String finalpassword=salt+"&"+mdpassword;
       return finalpassword;
    }
    public static boolean decypt(String password,String finalpassword){
        String[] arr=finalpassword.split("&");
        String salt=arr[0];
        String mdpassword=arr[1];
        String newmdpassword= DigestUtils.md5DigestAsHex((password+salt).getBytes());
        if(!mdpassword.equals(newmdpassword)){
            return false;
        }
        return true;

    }
}
